 /*
  ************************************************************************************************
  *
  *  File:          main.c
  *
  *  Compilers:     WinAVR
  *
  *  Controller:    Atmega328p, Inc 8MHz
  *
  *  Programmer:    @TH
  *                
  *
  *  Description:   
  *
  ************************************************************************************************
  */
 

#include <osa.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>

#include "main.h"
#include "serial.h"
#include "keyin.h"
#include "disp.h"
#include "rtc.h"
#include "manage.h"

//******************************************************************************
//  
//      Constants
// 
//******************************************************************************




//------------------------------------------------------------------------------
// Timers
//------------------------------------------------------------------------------

    enum{
        STOP             = 0,
        CK_DIV1          = 1,
        CK_DIV8          = 2,
        CK_DIV64         = 3,
        CK_DIV256        = 4,
        CK_DIV1024       = 5,
        FALLING_EDGE     = 6,
        RISING_EDGE      = 7
    };

//   Timer1 used for system ticks
//   F_CPU / CLK_DIV1 =  8000000/1 = 16000000 = 8E6
//   Fosc / second  =  1/8000000 = 0.125E-6
//   caculate 1us reload (1/1000000)/0.00625us = 1E-6 / 1.25E-6 = 8
//   TCNT0 = 8 for timer0 count 1us        
//   default  INTS_PER_milSECOND = 8  --- 1 us
//   default  INTS_PER_milSECOND = 800  --- 100 us
//   default  INTS_PER_milSECOND = 8000  --- 1000 us = 1ms
#define TCCR1B_CONST     CK_DIV1     // prs = 1
// #define TCNT1_CONST      8000       // 1000 us = 1ms
#define TCNT1_CONST      2000       // 1000 us = 1ms

#define LED_PORT 	PORTD
#define LED_PIN  	PIND
#define LED_DIR  	DDRD
#define LED  		PD4

//******************************************************************************
//  
//      Global variables
// 
//******************************************************************************



//******************************************************************************
//  
//      Function prototypes
// 
//******************************************************************************

void    InitMain (void);
void    TaskLED (void);

/********************************************************************************
 *                                                                              *
 *  Function:       main                                                        * 
 *                                                                              *
 *------------------------------------------------------------------------------* 
 *                                                                              *
 ********************************************************************************/

int  main (void)
{
    InitMain();                     // Init periphery

	// InitSerial();					//Init serial
	
	InitKeyIN();					//Init keyin
	
	InitDisp(); 					//Init Disp
	
	InitRTC(); 						//Init RTC
	
	InitManage();					//Init Manage
	
	OS_Init();                      // Init OS
	
    OS_Task_Create(0, TaskLED);    	// Create tasks.
    // OS_Task_Create(0, TaskSerial); 	 // Create tasks.
    OS_Task_Create(0, TaskKeyIN); 	 // Create tasks.
    OS_Task_Create(0, TaskDisp);  	// Create tasks.
    OS_Task_Create(0, TaskRTC);  	// Create tasks.
    OS_Task_Create(0, TaskManage);  	// Create tasks.

    OS_EI();                        // Enable interrupts

    OS_Run();                        // Running scheduler
}


/********************************************************************************
 *
 *  Interrupts service routine
 *
 ********************************************************************************/
ISR(TIMER1_OVF_vect)
{
    //******************************************************************************
    //  System timer (for system ticks)
    //******************************************************************************

	TCNT1 -= TCNT1_CONST; 
    OS_Timer();

}


/********************************************************************************
 *                                                                              *
 *  TASK:           Task_LED1                                                   * 
 *                                                                              *
 *------------------------------------------------------------------------------*
 *
 *  description:    
 *
 ********************************************************************************/

void TaskLED (void)
{

    for (;;)
    {	
		OS_Delay(500 ms);
		LED_PORT ^= _BV(LED);                              // invert led
    }
}



/********************************************************************************
 *                                                                              *
 *  Function:       InitMain                                                        *
 *                                                                              *
 *------------------------------------------------------------------------------* 
 *
 *  description:    Makes periphery initialization:
 *                  - I/O ports
 *                  - ADC
 *                  - timers
 *                  - interrupts
 *
 *  parameters:     void
 *
 *  on return:      void
 *
 ********************************************************************************/

void InitMain (void)
{
	OSCCAL = eeprom_read_byte( (uint8_t*)EPPROM_OSCCAL_ADDR );
	MCUCR = _BV(SE);
	TCCR1B = TCCR1B_CONST;
	TCNT1  = TCNT1_CONST ;

	DDRD |= _BV(PD5);

	TIMSK1 = _BV(TOIE1);	

}


//******************************************************************************
//  THE END
//******************************************************************************



